package com.example.cunion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cunion.entity.Dish;
import com.example.cunion.exception.CunionException;
import com.example.cunion.mapper.DishMapper;
import com.example.cunion.service.DishService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<HashMap> searchDishesByShopId(HashMap map) {
        //获取shopId
        String shopId = map.get("shopId").toString();
        //获取start
        long start = Long.parseLong(map.get("start").toString());
        //获取length
        long length = Long.parseLong(map.get("length").toString());
        //计算end
        long end = start + length - 1;
        //获取dish数量
        Long size = redisTemplate.opsForList().size("dish:AllDish:" + shopId);
        //如果dish数量大于0，则从redis中获取dish
        if (size > 0){
            List range = redisTemplate.opsForList().range("dish:AllDish:" + shopId, start, end);
            return range;
        }
        //从数据库中获取dish
        ArrayList<HashMap> maps = dishMapper.searchDishesByShopId(map);
        //将dish同步到redis
        ArrayList<HashMap> list = dishMapper.syncDishesByShopId(map);
        //将dish同步到redis
        for (HashMap hashMap : list){
            redisTemplate.opsForList().rightPush("dish:AllDish:" + shopId, hashMap);
        }
        //设置redis过期时间
        redisTemplate.expire("dish:AllDish:" + shopId, 1, TimeUnit.HOURS);
        return maps;
    }

    @Override
    public Integer addDish(HashMap map) {
        String shopId = map.get("shopId").toString();
        redisTemplate.delete("dish:AllDish:" + shopId);
        Integer result = dishMapper.addDish(map);
        redisTemplate.delete("dish:AllDish:" + shopId);
        if (result != 1){
            throw new CunionException("菜品添加失败！");
        }
        return result;
    }

    @Override
    public Integer addDish(Dish dish) {
        int insert = dishMapper.insert(dish);
        if (insert != 1){
            throw new CunionException("添加失败");
        }
        return insert;
    }

    @Override
    public Integer deletedDish(String dishId) {
        int result = dishMapper.deleteById(dishId);
        Set keys = redisTemplate.keys("dish:AllDish:*");
        if (keys != null){
            redisTemplate.delete(keys);
        }
        if (result != 1){
            throw new CunionException("删除失败！");
        }
        return result;
    }

    @Override
    public Integer updateDish(Dish dish) {
        int result = dishMapper.updateById(dish);
        if (result != 1){
            throw new CunionException("修改失败");
        }
        return result;
    }

    @Override
    public List<Dish> searchDish(Integer start, Integer length) {
        Page<Dish> page = new Page<>(start, length);
        QueryWrapper<Dish> QueryWrapper = new QueryWrapper<>();
        Page<Dish> selectPage = dishMapper.selectPage(page, QueryWrapper);
        List<Dish> list = selectPage.getRecords();
        return list;
    }

}
